package com.mayuanfei.springproxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * JDK代理类
 *
 * @author 老马
 * @date 2023-02-28 10:50
 */
public class PersionJdkProxy implements InvocationHandler {

    private Object persionObj;

    public PersionJdkProxy(Object persionObj) {
        this.persionObj = persionObj;
    }

    /**
     * proxy : 代理对象
     * method: 被代理对象的方法
     * args: 被代理对象方法运行时的参数
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        if(method.getName().equals("eat")){
            System.out.println("吃饭前洗手");
            result = method.invoke(this.persionObj, args);
            System.out.println("吃饭后洗碗");
        }else {
            // 其他方法正常执行
            result = method.invoke(this.persionObj, args);
        }
        return result;
    }
}